<?php
   class Model_hotel extends CI_Model{
		protected $_table = "tbl_hotel";
		public function __construct(){
			parent::__construct();
			$this->load->database();
		}
		public function getdata($id){
			$this->db->where("id",$id);
			return $this->db->get($this->_table)->row_array();
		}
		public function listall($limit,$start){
			$this->db->limit($limit,$start);
			return $this->db->get($this->_table)->result_array();
		}
		public function add($data){
        	$this->db->insert($this->_table,$data);
		}
		public function del($id){
			$this->db->where("id",$id);
			$this->db->delete($this->_table);
		}
		public function update($data,$id){
			$this->db->where("id",$id);
			$this->db->update($this->_table,$data);
		}
		
		
		//public function getall_byhotelid($id,$datetime){
//			$query=$this->db->query(" SELECT a.id as hotelid,a.name, a.`overview` , a.`facility` , b.id as roomid,b.name AS roomname, b.avatar, b.description, b.maxperson, c. price,c.fromdate,c.todate FROM `tbl_hotel` AS a INNER JOIN tbl_hotelroom AS b ON a.id = b.hotelid INNER JOIN tbl_hotelroomprice AS c ON c.roomid = b.id WHERE a.id =".$id." and c.fromdate<='".$datetime."' and c.todate>='".$datetime."'");
//			return $query->result_array();
//		}
		
		//public function gethotelbyroomid($select,$id){
//			$query=$this->db->query("select ".$select." from tbl_hotel where id in (select hotelid from tbl_hotelroom where id=".$id.")");
//			return $query->result_array();
//		}
		
		//public function getcolunm_allhotel($colunm){
//			$this->db->select($colunm);	
//			return $this->db->get($this->_table)->result_array();
//		}
		//public function listhotel($name,$numberroom,$numberperson,$fromdate,$todate,$limit,$start){
//			$query=$this->db->query("select a.id as hotelid,a.category,a.name,a.provide,a.address,b.id as roomid,b.maxperson,b.avatar,b.name as roomname,b.description,c.price,c.oldprice from tbl_hotel as a inner join tbl_hotelroom as b on a.id=b.hotelid inner join tbl_hotelroomprice as c on b.id=c.roomid where a.name like '%".$name."%' limit ".$start.",".$limit);
//			return $query->result_array();
//		}
//			
		public function listtopbyprovide_orderby($limit,$start,$provide,$orderby){						
			$query=$this->db->query("select * from tbl_hotel where provide like '%".$provide."%' order by ".$orderby." limit ".$start.",".$limit);
			return $query->result_array();
		}
		
		
		
		//public function countallbyprovide($provide){
//			$query=$this->db->query("select count(*) as total from tbl_hotel where provide like '%".$provide."%'");
//			return $query->row_array();
//		}
//		
//		public function countlisthotel($name,$numberroom,$numberperson,$fromdate,$todate){
//			$query=$this->db->query("select count(*) as total from tbl_hotel as a inner join tbl_hotelroom as b on a.id=b.hotelid inner join tbl_hotelroomprice as c on b.id=c.roomid where a.name like '%".$name."%'");
//			return $query->row_array();
//		}
//		
//		public function count_search(){
//			$query=$this->db->query("select count(*) as total from tbl_hotel");
//			return $query->row_array();	
//		}
//
//		
		public function getallbyhotel_room($hotelid,$roomid,$from){
			$query=$this->db->query("SELECT a. *, b.type ,c.description as hoteldes, c.name AS hotelname FROM tbl_hotelroomprice AS a INNER JOIN tbl_hotelroom AS b ON a.roomid = b.id INNER JOIN tbl_hotel AS c ON b.hotelid = c.id WHERE a.roomid =".$roomid." AND c.id =".$hotelid." and (a.fromdate<='".$from."' and a.todate>='".$from."') order by a.price LIMIT 0 , 1");
			return $query->row_array();
		}
//		
//		public function getminpricebyhotelid($id){
//			$query=$this->db->query("select c.price from tbl_hotelroom as a inner join tbl_hotelroomprice as c on a.id=c.roomid where a.hotelid=".$id." order by price limit 1");					   			return $query->row_array();
//		}
//		
//		public function getsearchtophotelbyprovide($provide,$limit,$start,$orderby){
//			$query=$this->db->query("select * from tbl_hotel where provide like '%".$provide."%' order by ".$orderby." limit ".$start.",".$limit);
//			return $query->result_array();
//		}
//		public function getsearchtophotelbyprovide_star($star,$provide,$limit,$start,$orderby){
//			$query=$this->db->query("select * from tbl_hotel where provide like '%".$provide."%' and star=".$star." order by ".$orderby." limit ".$start.",".$limit);
//			return $query->result_array();
//		}
//		public function getsearchtophotelbyprovide_services($cuphap,$provide,$limit,$start){
//			$query=$this->db->query("select * from tbl_hotel where provide like '%".$provide."%' and ".$cuphap." order by service limit ".$start.",".$limit);
//			return $query->result_array();
//		}
//		public function getsearchtophotelbyprovide2($provide,$limit,$start){
//			$query=$this->db->query("select * from tbl_hotel where provide like '%".$provide."%' limit ".$start.",".$limit);
//			return $query->result_array();
//		}
//		public function getallhotelbyprovide($provide){
//			$query=$this->db->query("select * from tbl_hotel where provide like '%".$provide."%'");
//			return $query->result_array();
//		}
//		
//
//		public function getTopHotelByProvideOrderbyrecommendations($provide,$from,$limit,$start){
//			$query=$this->db->query("select h.*, (SELECT price FROM tbl_hotelroomprice WHERE roomid IN (SELECT id FROM tbl_hotelroom WHERE hotelid =h.id) AND fromdate <= '".$from."' order by price limit 1) as minprice from tbl_hotel as h where h.provide like '%".$provide."%'  order by h.recommendations limit ".$start.",".$limit);		
//			return $query->result_array();
//		}
//		public function getTopHotelByProvideOrderbyPrice($provide,$to,$from,$limit,$start){
//			$query=$this->db->query("select h.*, (SELECT price FROM tbl_hotelroomprice WHERE roomid IN (SELECT id FROM tbl_hotelroom WHERE hotelid =h.id) AND todate >= '".$to."' AND fromdate <= '".$from."' order by price limit 1) as minprice from tbl_hotel as h where h.provide like '%".$provide."%'  order by minprice limit ".$start.",".$limit);			
//			return $query->result_array();
//		}
//		
//		public function getTopHotelByProvide_MinPriceOrderbyPrice($provide,$price,$to,$from,$limit,$start){
//			$query=$this->db->query("select h.*, (SELECT price FROM tbl_hotelroomprice WHERE roomid IN (SELECT id FROM tbl_hotelroom WHERE hotelid =h.id) AND todate >= '".$to."' AND fromdate <= '".$from."' order by price limit 1) as minprice from tbl_hotel as h where h.provide like '%".$provide."%' and minprice<=".$price."  order by minprice limit ".$start.",".$limit);			
//			return $query->result_array();	
//		}
		
//		
//		
		public function getallbanner(){
			$query=$this->db->query("select * from tbl_hotel where status=1 order by id desc");	
			return $query->result_array();
		}
		
		// used in file "hotel_search_result"
		public function getallbyProvide_Name($provide,$name){
			$select="select * from tbl_hotel ";
			$order=" order by name ";
			$where=" where 1=1 ";
			if(isset($provide) && $provide!='') $where.=" and provide like '%".$provide."%'";
			if(isset($name) && $name!='') $where.=" and name like '%".$name."%'";
			$query=$this->db->query($select.$where.$order);
			return $query->result_array();
		}

		public function countallhotelbyprovide($provide){
			$query=$this->db->query("select count(*) as total from tbl_hotel where provide like '%".$provide."%'");
			return $query->row_array();
		}

		public function priceminest($id){
			$priceminest=$this->db->query("SELECT c.price FROM `tbl_hotelroomprice` as a inner join tbl_hotelroom as b on a.id=b.hotelid inner join tbl_hotelroomprice as c on b.id=c.roomid where a.id=".$id." order by price limit 1");
			return $priceminest->result_array();
		}
		
		public function getallbyhotelid($id){
			$query=$this->db->query("select a.*,c.* from tbl_hotel as a inner join tbl_hotelroom as b on a.id=b.hotelid inner join tbl_hotelroomprice as c on b.id=c.roomid where a.id=".$id." order by c.price limit 1");			
			return $query->row_array();
		}
		
		public function gettopbyid($hotelid,$provide,$hotelstar,$limit,$start){
		  $query=$this->db->query("select * from tbl_hotel where id!=".$hotelid." and provide like '%".$provide."%' and star=".$hotelstar." order by name limit ".$start.",".$limit);	
		  return $query->result_array();
		}

		public function get_all_name(){
			$this->db->query("select name from tbl_hotel order by name");	
			return $this->db->get($this->_table)->result_array();
		}

		public function get_search_ajax($provide,$from,$service,$star,$price,$priority,$hotelname,$limit,$start){		
			if($priority=="price"){
				$order=" order by minprice ";
			}else $order=" order by h.".$priority." ";
			$limited=" limit ".$start.",".$limit;
			$query="select h.*, (SELECT price FROM tbl_hotelroomprice WHERE roomid IN (SELECT id FROM tbl_hotelroom WHERE hotelid =h.id) and fromdate<='".$from."' and todate>='".$from."' ".$price." order by price limit 1) as minprice,(SELECT id FROM tbl_hotelroomprice WHERE roomid IN (SELECT id FROM tbl_hotelroom WHERE hotelid =h.id) and fromdate<='".$from."' and todate>='".$from."' ".$price." order by price limit 1) as hotelroompriceid,(SELECT oldprice FROM tbl_hotelroomprice WHERE roomid IN (SELECT id FROM tbl_hotelroom WHERE hotelid =h.id) and fromdate<='".$from."' and todate>='".$from."' ".$price." order by price limit 1) as oldprice from tbl_hotel as h ";			
			$where="where h.provide like '%".$provide."%'";
			if(isset($service)) $where.= $service;	
			if(isset($star)) $where.= $star;
			if(isset($hotelname)) $where.= $hotelname;
			$query2=$this->db->query($query.$where.$order.$limited);
			return $query2->result_array();
		}
	}
	?>